package com.sora.org.controller;

import com.sora.basic.annotation.PreAuthorize;
import com.sora.basic.util.AjaxResult;
import com.sora.basic.util.PageList;
import com.sora.org.domain.Employee;
import com.sora.org.query.EmployeeQuery;
import com.sora.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/employee")
@Api(value="部门接口类",description = "处理部门的CRUD操作")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/employee/1
    @GetMapping("/{id}")
    @ApiOperation(value = "接口：查询单个")
    @PreAuthorize(name = "员工查询单个",sn = "employee:getOne")
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //http://localhost:8080/employee
    @GetMapping
    @ApiOperation(value = "接口：查询所有")
    @PreAuthorize(name = "员工查询所有",sn = "employee:getAll")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口：删除")
    @PreAuthorize(name = "员工删除",sn = "employee:delete")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或者修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "接口：添加或者修改")
    @PreAuthorize(name = "员工保存",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询+高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "接口：分页查询+高级查询")
    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "接口：批量删除")
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 接单之后选择接送人的接口
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
